﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using W.Warehouse.Api.Application.Command.AllocationCommand;
using W.Warehouse.Domain.AllocationProduct;

namespace W.Warehouse.Api.Controllers
{
    /// <summary>
    /// 调拨管理控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class AllocationController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ILogger<AllocationController> _logger;   
        public AllocationController(IMediator mediator, ILogger<AllocationController> logger)
        {
            _mediator = mediator;
            _logger = logger;
        }
        /// <summary>
        /// 新增调拨管理
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddAllocationAsync(AllocationCreateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                _logger.LogInformation("调用新增调拨管理方法");
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                var name = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Name)?.Value;//获取名称
                command.CreateName = name;
                command.userid = Convert.ToInt32(uid);
                command.officeId = sid;
                result.Code = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                _logger.LogError("调用新增调拨管理方法时出错"+ex.Message);
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 删除调拨管理
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DeleteAllocationAsync(AllocationDeleteCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                _logger.LogInformation("调用删除调拨管理方法");
                result.Code = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                _logger.LogError("调用删除调拨管理方法时出错" + ex.Message);
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 批量删除调拨管理
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DeleteRangeAllocationAsync(AllocationDeleteRangeCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                _logger.LogInformation("调用批量删除调拨管理方法");
                result.Code = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                _logger.LogError("调用批量删除调拨管理方法时出错" + ex.Message);
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 修改调拨管理
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateAllocationAsync(AllocationUpdateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                _logger.LogInformation("调用修改调拨管理方法");
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                var name = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Name)?.Value;//获取名称
                command.CreateName = name;
                command.userid = Convert.ToInt32(uid);
                command.officeId = sid;
                command.UpdateName = name;
                result.Code = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                _logger.LogError("调用修改调拨管理方法时出错" + ex.Message);
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 审核调拨管理
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AuditAllocationAsync(AllocationAuditCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                _logger.LogInformation("调用审核调拨管理方法");
                result.Code = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                _logger.LogError("调用审核调拨管理方法时出错" + ex.Message);
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
    }
}
